<?php

namespace app\wap\controller;

use app\common\model\Idea;
use app\common\model\User10Extend;
use think\exception\ValidateException;
use think\facade\Db;

class User extends Base
{
    /**
     * getInfo #获取用户信息
     * api get /{controller}/{method}
     *
     * @auth kouweihong
     * @date 2020/12/2
     * @throws \Exception
     */
    public function getInfo()
    {
        $this->success($this->auth);
    }

    /**
     * updatePwd #修改密码
     * api post /{controller}/{method}
     * @param pwd string 原密码
     * @param npwd string 新密码
     * @param cnpwd string 重复新密码
     *
     * @auth kouweihong
     * @date 2020/12/2
     */
    public function updatePwd()
    {
        $this->request->isPost() || $this->error('非法操作');
        $params = input('post.');
        $params || $this->error('提交参数为空');

        try {
            $params['pwd'] = \app\common\model\User::encryptPassword($params['pwd']);
            $params['password'] = $this->auth->password;
            validate(['pwd|原密码' => 'require|confirm:password', 'npwd|新密码' => 'require', 'cnpwd|重复新密码' => 'require|confirm:npwd'])->check($params);

            //更新密码
            $res = $this->auth->save(['password' => \app\common\model\User::encryptPassword($params['npwd'])]);
            $res === false && $this->error('操作失败');

            $this->success();
        } catch (ValidateException $e) {
            $this->error($e->getMessage());
        }
    }

    /**
     * updateInfo #修改基本信息
     * api post /{controller}/{method}
     *
     * @auth kouweihong
     * @date 2020/12/4
     */
    public function updateInfo()
    {
        $this->request->isPost() || $this->error('非法操作');
        $params = input('post.');
        $params || $this->error('提交参数为空');

        try {
            validate(['nickname' => 'require', 'true_name' => 'require'])->check($params);

            Db::startTrans();

            //保存
            $res = $this->auth->save($params);
            if ($res === false) {
                Db::rollback();
                $this->error('操作失败');
            }

            //保存
            $res = $this->auth->user10->save($params);
            if ($res === false) {
                Db::rollback();
                $this->error('操作失败');
            }

            Db::commit();
            $this->success();
        } catch (ValidateException $e) {
            $this->error($e->getMessage());
        }
    }

    /**
     * submitIdea #提交意见
     * api post /{controller}/{method}
     * @param name string 称呼
     * @param phone string 联系电话
     * @param email string 联系邮箱
     * @param desc string 描述
     *
     * @auth kouweihong
     * @date 2020/12/2
     */
    public function submitIdea()
    {
        $this->request->isPost() || $this->error('非法操作');
        $params = input('post.');
        $params || $this->error('提交参数为空');

        try {
            validate(['name|称呼' => 'require', 'phone|联系电话' => 'require', 'email|手机' => 'require', 'desc|描述' => 'require'])->check($params);

            //更新密码
            $params['ip'] = $this->request->ip();
            $idea = Idea::create($params);
            (!$idea || !$idea->id) && $this->error('操作失败');

            $this->success();
        } catch (ValidateException $e) {
            $this->error($e->getMessage());
        }
    }
}
